<script setup lang="ts">
import { localCache } from '@/utils';
import { ElForm } from 'element-plus';
import { defineExpose, reactive, ref } from 'vue';
import { useStore } from 'vuex';

import { rules } from '../config/account-config';

const account = reactive({
  name: localCache.getCache('name') ?? '',
  password: localCache.getCache('password') ?? ''
});

const formRef = ref<InstanceType<typeof ElForm>>();

const store = useStore();
const loginAction = (isKeepPassword: boolean) => {
  formRef.value?.validate((valid) => {
    if (valid) {
      if (isKeepPassword) {
        localCache.setCache('name', account.name);
        localCache.setCache('password', account.password);
        store.dispatch('login/accountLoginAction', { ...account });
      } else {
        localCache.deleteCache('name');
        localCache.deleteCache('password');
      }
      console.log('真正执行登录逻辑');
    }
  });
};
defineExpose({
  loginAction
});
</script>
<template>
  <div class="login-account">
    <el-form label-width="60px" :model="account" :rules="rules" ref="formRef">
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>
<style lang="" scoped></style>
